﻿using Archives.Models;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Archives.DbHelper;
using Archives.Entities;
using Archives.Repositories;
using Archives.IService;

namespace Archives.Controllers
{
    /// <summary>
    /// 工作流页面
    /// </summary>
    public class WorkFlowPageController : BaseController
    {
        private readonly IWorkflowQueryService _service;

        public WorkFlowPageController(IWorkflowQueryService service)
        {
            _service = service;
        }
        /// <summary>
        /// 待处理
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetPendingInfo(QueryInfo query)
        {
            //当前人
            var emplId = (User.Identity as Appkiz.Library.Security.Authentication.AppkizIdentity).Employee.EmplID;

            var sql = new StringBuilder();
            sql.Append(string.Format(@"SELECT ROW_NUMBER () OVER (ORDER BY c.CreateTime DESC) number, b.DocumentNumber,
                                    b.DocumentTitle,
                                    CONVERT(varchar(100), b.ClosedOrHairTime, 20) as ClosedOrHairTime,
                                    b.MessageId,
                                    b.WorkFlowId,
                                    b.SequenceName,
                                    b.SequenceNumber,
                                    --流程发起人
                                    a.MessageIssuedBy AS InitiateEmplId,
                                    d.EmplName AS InitiateEmplName,
                                    --流程类型
                                    a.MessageTitle,
                                    --环节
                                    c.NodeName AS MyTask,
                                    CONVERT(varchar(100), c.CreateTime, 20) as ReceiveTime
                                     FROM WKF_Message a
                                    INNER JOIN GW_ReceiveFile b
                                    ON a.MessageID = b.MessageId
                                    INNER JOIN WKF_MessageHandle c
                                    ON a.MessageID = c.MessageID
                                    INNER JOIN ORG_Employee d
                                    ON a.MessageIssuedBy = d.EmplID
                                    WHERE c.HandleStatus != 0
                                    and b.IsFirstLoading = 0
                                    and a.MessageStatus != 3 
                                    and (c.UserID = '{0}' or (c.EntrustBy = '{0}' and c.EntrustBy <> ''))", emplId));
            if (query.PageNumber <= 0)
            {
                query.PageNumber = 1;
            }
            if (query.PageSize <= 0)
            {
                query.PageSize = 20;
            }
            if (!string.IsNullOrWhiteSpace(query.SortDirection))
            {
                query.SortDirection = "desc";
            }
            if (!string.IsNullOrWhiteSpace(query.SortColumn))
            {
                query.SortColumn = "ReceiveTime";
            }
            //公文标题
            if (!string.IsNullOrWhiteSpace(query.Title))
            {
                sql.Append(string.Format(@" and b.DocumentTitle like '%{0}%'", query.Title));
            }
            //文号
            if (!string.IsNullOrWhiteSpace(query.DocumentNumber))
            {
                sql.Append(string.Format(@" and b.DocumentNumber like '%{0}%'", query.DocumentNumber));
            }
            //开始发文时间
            if (query.StartClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime >= '{0}'", query.StartClosedOrHairTime));
            }
            //结束发文时间
            if (query.EndClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime <= '{0}'", query.EndClosedOrHairTime));
            }
            //开始接受时间
            if (query.StartReceiveTime.HasValue)
            {
                sql.Append(string.Format(@" and c.CreateTime >= '{0}'", query.StartReceiveTime));
            }
            //结束接收时间
            if (query.EndReceiveTime.HasValue)
            {
                sql.Append(string.Format(@" and c.CreateTime <= '{0}'", query.EndReceiveTime));
            }
            //流程类型
            if (!string.IsNullOrWhiteSpace(query.WorkFlowId))
            {
                sql.Append(string.Format(@" and b.WorkFlowId = '{0}'", query.WorkFlowId));
            }
            //序列名称
            if (!string.IsNullOrWhiteSpace(query.SequenceName))
            {
                sql.Append(string.Format(@" and b.SequenceName = '{0}'", query.SequenceName));
            }
            //序列号
            if (!string.IsNullOrWhiteSpace(query.SequenceNumber))
            {
                sql.Append(string.Format(@" and b.SequenceNumber like '%{0}%'", query.SequenceNumber));
            }
            //模糊检索
            if (!string.IsNullOrWhiteSpace(query.QueryCondition))
            {
                sql.Append(string.Format(@" and (b.DocumentNumbe like '%{0}%' or b.DocumentTitle like '%{0}%')", query.QueryCondition));
            }


            var total = SqlPaging.SqlPaing(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var table = SqlPaging.TableInfo(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var result = new PendingInfo()
            {
                TotalInfo = total,
                Data = ModelConvertHelper<PendingPageInfo>.ConvertToModel(table).ToList()
            };
            return Json(result);
        }
        /// <summary>
        /// 发出未完成
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetNoCompleteInfo(QueryInfo query)
        {
            //当前人
            var emplId = (User.Identity as Appkiz.Library.Security.Authentication.AppkizIdentity).Employee.EmplID;

            var sql = new StringBuilder();
            sql.Append(string.Format(@"SELECT ROW_NUMBER () OVER (ORDER BY a.MessageCreateTime DESC) number, b.DocumentNumber,
                                     b.DocumentTitle,
                                     b.ClosedOrHairTime,
                                     b.MessageId,
                                     b.WorkFlowId,
                                     b.SequenceName,
                                     b.SequenceNumber,
                                     --流程类型
                                     a.MessageTitle,
                                     --环节
                                     c.NodeName AS MyTask,
                                     a.MessageCreateTime AS CreateTime
                                     FROM WKF_Message a
                                    INNER JOIN GW_ReceiveFile b
                                    ON a.MessageID = b.MessageId
                                    INNER JOIN WKF_MessageHandle c
                                    ON a.MessageID = c.MessageID
                                    WHERE a.MessageStatus != 2
                                    AND c.HandleStatus != 0
                                    and a.MessageStatus != 3 
                                    and b.IsFirstLoading = 0
                                    and a.MessageIssuedBy = '{0}'", emplId));
            var groupInfo = string.Format(@" GROUP BY a.MessageID, a.MessageCreateTime,
									b.DocumentNumber,
                                     b.DocumentTitle,
                                     b.ClosedOrHairTime,b.MessageId,
                                     b.WorkFlowId,
                                     b.SequenceName,
                                     b.SequenceNumber,
									--流程类型
                                     a.MessageTitle,
                                     --环节
                                     c.NodeName,
                                     a.MessageCreateTime ");
            if (query.PageNumber <= 0)
            {
                query.PageNumber = 1;
            }
            if (query.PageSize <= 0)
            {
                query.PageSize = 20;
            }
            //公文标题
            if (!string.IsNullOrWhiteSpace(query.Title))
            {
                sql.Append(string.Format(@" and b.DocumentTitle like '%{0}%'", query.Title));
            }
            //文号
            if (!string.IsNullOrWhiteSpace(query.DocumentNumber))
            {
                sql.Append(string.Format(@" and b.DocumentNumber like '%{0}%'", query.DocumentNumber));
            }
            //开始发文时间
            if (query.StartClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime >= '{0}'", query.StartClosedOrHairTime));
            }
            //结束发文时间
            if (query.EndClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime <= '{0}'", query.EndClosedOrHairTime));
            }
            //开始发起时间
            if (query.StartCreatedTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageCreateTime >= '{0}'", query.StartCreatedTime));
            }
            //结束发起时间
            if (query.EndCreatedTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageCreateTime <= '{0}'", query.EndCreatedTime));
            }
            //流程类型
            if (!string.IsNullOrWhiteSpace(query.WorkFlowId))
            {
                sql.Append(string.Format(@" and b.WorkFlowId = '{0}'", query.WorkFlowId));
            }
            //序列名称
            if (!string.IsNullOrWhiteSpace(query.SequenceName))
            {
                sql.Append(string.Format(@" and b.SequenceName = '{0}'", query.SequenceName));
            }
            //序列号
            if (!string.IsNullOrWhiteSpace(query.SequenceNumber))
            {
                sql.Append(string.Format(@" and b.SequenceNumber like '%{0}%'", query.SequenceNumber));
            }
            sql.Append(groupInfo);
            var total = SqlPaging.SqlPaing(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var table = SqlPaging.TableInfo(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var result = new NoCompleteInfo()
            {
                TotalInfo = total,
                Data = ModelConvertHelper<CreateNoCompleteInfo>.ConvertToModel(table).ToList() ?? null
            };
            return JsonDate(result);
        }
        /// <summary>
        /// 发出已完成
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetCompleteInfo(QueryInfo query)
        {
            //当前人
            var emplId = (User.Identity as Appkiz.Library.Security.Authentication.AppkizIdentity).Employee.EmplID;

            var sql = new StringBuilder();
            sql.Append(string.Format(@"SELECT ROW_NUMBER () OVER (ORDER BY a.MessageCreateTime DESC) number, b.DocumentNumber,
                                     b.DocumentTitle,
                                     b.ClosedOrHairTime,
                                     b.MessageId,
                                     b.WorkFlowId,
                                     b.SequenceName,
                                     b.SequenceNumber,
                                     --流程类型
                                     a.MessageTitle,
                                     a.MessageCreateTime AS CreateTime,
                                     a.MessageFinishTime AS FinishTime
                                     FROM WKF_Message a
                                    INNER JOIN GW_ReceiveFile b
                                    ON a.MessageID = b.MessageId
                                    WHERE a.MessageStatus = 2
                                    and b.IsFirstLoading = 0
                                    and a.MessageIssuedBy = '{0}'", emplId));
            if (query.PageNumber <= 0)
            {
                query.PageNumber = 1;
            }
            if (query.PageSize <= 0)
            {
                query.PageSize = 20;
            }
            //公文标题
            if (!string.IsNullOrWhiteSpace(query.Title))
            {
                sql.Append(string.Format(@" and b.DocumentTitle like '%{0}%'", query.Title));
            }
            //文号
            if (!string.IsNullOrWhiteSpace(query.DocumentNumber))
            {
                sql.Append(string.Format(@" and b.DocumentNumber like '%{0}%'", query.DocumentNumber));
            }
            //开始发文时间
            if (query.StartClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime >= '{0}'", query.StartClosedOrHairTime));
            }
            //结束发文时间
            if (query.EndClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime <= '{0}'", query.EndClosedOrHairTime));
            }
            //开始发起时间
            if (query.StartCreatedTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageCreateTime >= '{0}'", query.StartCreatedTime));
            }
            //结束发起时间
            if (query.EndCreatedTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageCreateTime <= '{0}'", query.EndCreatedTime));
            }
            //开始完成时间
            if (query.StartEndTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageFinishTime >= '{0}'", query.StartEndTime));
            }
            //结束完成时间
            if (query.EndEndTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageFinishTime <= '{0}'", query.EndEndTime));
            }
            //流程类型
            if (!string.IsNullOrWhiteSpace(query.WorkFlowId))
            {
                sql.Append(string.Format(@" and b.WorkFlowId = '{0}'", query.WorkFlowId));
            }
            //序列名称
            if (!string.IsNullOrWhiteSpace(query.SequenceName))
            {
                sql.Append(string.Format(@" and b.SequenceName = '{0}'", query.SequenceName));
            }
            //序列号
            if (!string.IsNullOrWhiteSpace(query.SequenceNumber))
            {
                sql.Append(string.Format(@" and b.SequenceNumber like '%{0}%'", query.SequenceNumber));
            }
            var total = SqlPaging.SqlPaing(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var table = SqlPaging.TableInfo(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var result = new CompleteInfo()
            {
                TotalInfo = total,
                Data = ModelConvertHelper<CreateCompleteInfo>.ConvertToModel(table).ToList() ?? null
            };
            return JsonDate(result);
        }

        /// <summary>
        /// 处理过
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetDealWithInfo(QueryInfo query)
        {
            //当前人
            var emplId = (User.Identity as Appkiz.Library.Security.Authentication.AppkizIdentity).Employee.EmplID;

            var sql = new StringBuilder();
            sql.Append(string.Format(@"SELECT ROW_NUMBER () OVER (ORDER BY d.HandledTime DESC) number, b.DocumentNumber,
                                    b.DocumentTitle,
                                    b.ClosedOrHairTime,
                                    b.MessageId,
                                    b.WorkFlowId,
                                    b.SequenceName,
                                    b.SequenceNumber,
                                    --流程类型
                                    a.MessageTitle,
                                    a.MessageCreateTime AS CreateTime,
                                    d.HandledTime AS ReceiveTime
                                     FROM WKF_Message a
                                    INNER JOIN GW_ReceiveFile b
                                    ON a.MessageID = b.MessageId
                                    INNER JOIN (select MAX(HandledTime) as HandledTime,MessageID,HandledBy from WKF_WorkflowHistory 
									where NodeKey != 'NODE0001' group by MessageID,HandledBy) as d
                                    ON a.MessageID = d.MessageID
                                    WHERE d.HandledBy = '{0}'  
                                    and a.MessageStatus != 3
                                    and b.IsFirstLoading = 0", emplId));
            if (query.PageNumber <= 0)
            {
                query.PageNumber = 1;
            }
            if (query.PageSize <= 0)
            {
                query.PageSize = 20;
            }
            //公文标题
            if (!string.IsNullOrWhiteSpace(query.Title))
            {
                sql.Append(string.Format(@" and b.DocumentTitle like '%{0}%'", query.Title));
            }
            //文号
            if (!string.IsNullOrWhiteSpace(query.DocumentNumber))
            {
                sql.Append(string.Format(@" and b.DocumentNumber like '%{0}%'", query.DocumentNumber));
            }
            //开始发文时间
            if (query.StartClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime >= '{0}'", query.StartClosedOrHairTime));
            }
            //结束发文时间
            if (query.EndClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime <= '{0}'", query.EndClosedOrHairTime));
            }
            //开始发起时间
            if (query.StartCreatedTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageCreateTime >= '{0}'", query.StartCreatedTime));
            }
            //结束发起时间
            if (query.EndCreatedTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageCreateTime <= '{0}'", query.EndCreatedTime));
            }
            //开始接收时间
            if (query.StartReceiveTime.HasValue)
            {
                sql.Append(string.Format(@" and d.HandledTime <= {0}", query.StartReceiveTime));
            }
            //结束接收时间
            if (query.EndReceiveTime.HasValue)
            {
                sql.Append(string.Format(@" and d.HandledTime >= {0}", query.EndReceiveTime));
            }
            //流程类型
            if (!string.IsNullOrWhiteSpace(query.WorkFlowId))
            {
                sql.Append(string.Format(@" and b.WorkFlowId = '{0}'", query.WorkFlowId));
            }
            //序列名称
            if (!string.IsNullOrWhiteSpace(query.SequenceName))
            {
                sql.Append(string.Format(@" and b.SequenceName = '{0}'", query.SequenceName));
            }
            //序列号
            if (!string.IsNullOrWhiteSpace(query.SequenceNumber))
            {
                sql.Append(string.Format(@" and b.SequenceNumber like '%{0}%'", query.SequenceNumber));
            }
            //模糊检索
            if (!string.IsNullOrWhiteSpace(query.QueryCondition))
            {
                sql.Append(string.Format(@" and (b.DocumentNumbe like '%{0}%' or b.DocumentTitle like '%{0}%')", query.QueryCondition));
            }
            var total = SqlPaging.SqlPaing(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var table = SqlPaging.TableInfo(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var result = new DealInfo()
            {
                TotalInfo = total,
                Data = ModelConvertHelper<DealWithInfo>.ConvertToModel(table).ToList() ?? null
            };
            return JsonDate(result);
        }

        /// <summary>
        /// 终止信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetTerminationInfo(QueryInfo query)
        {
            //当前人
            var emplId = (User.Identity as Appkiz.Library.Security.Authentication.AppkizIdentity).Employee.EmplID;

            var sql = new StringBuilder();
            sql.Append(string.Format(@"SELECT ROW_NUMBER () OVER (ORDER BY a.MessageCreateTime DESC) number, b.DocumentNumber,
                                    b.DocumentTitle,
                                    b.ClosedOrHairTime,
                                    b.MessageId,
                                    b.WorkFlowId,
                                    b.SequenceName,
                                    b.SequenceNumber,
                                    --流程类型
                                    a.MessageTitle,
                                    a.MessageCreateTime AS CreateTime,
                                    a.MessageFinishTime AS FinishTime
                                     FROM WKF_Message a
                                    INNER JOIN GW_ReceiveFile b
                                    ON a.MessageID = b.MessageId
                                    WHERE a.MessageStatus = 3 
                                    and a.MessageIssuedBy = '{0}' 
                                    and b.IsFirstLoading = 0", emplId));
            if (query.PageNumber <= 0)
            {
                query.PageNumber = 1;
            }
            if (query.PageSize <= 0)
            {
                query.PageSize = 20;
            }
            //公文标题
            if (!string.IsNullOrWhiteSpace(query.Title))
            {
                sql.Append(string.Format(@" and b.DocumentTitle like '%{0}%'", query.Title));
            }
            //文号
            if (!string.IsNullOrWhiteSpace(query.DocumentNumber))
            {
                sql.Append(string.Format(@" and b.DocumentNumber like '%{0}%'", query.DocumentNumber));
            }
            //开始发文时间
            if (query.StartClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime >= '{0}'", query.StartClosedOrHairTime));
            }
            //结束发文时间
            if (query.EndClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime <= '{0}'", query.EndClosedOrHairTime));
            }
            //开始发起时间
            if (query.StartCreatedTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageCreateTime >= '{0}'", query.StartCreatedTime));
            }
            //结束发起时间
            if (query.EndCreatedTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageCreateTime <= '{0}'", query.EndCreatedTime));
            }
            //开始接收时间
            if (query.StartReceiveTime.HasValue)
            {
                sql.Append(string.Format(@" and d.HandledTime <= '{0}'", query.StartReceiveTime));
            }
            //结束接收时间
            if (query.EndReceiveTime.HasValue)
            {
                sql.Append(string.Format(@" and d.HandledTime >= '{0}'", query.EndReceiveTime));
            }
            //流程类型
            if (!string.IsNullOrWhiteSpace(query.WorkFlowId))
            {
                sql.Append(string.Format(@" and b.WorkFlowId = '{0}'", query.WorkFlowId));
            }
            //序列名称
            if (!string.IsNullOrWhiteSpace(query.SequenceName))
            {
                sql.Append(string.Format(@" and b.SequenceName = '{0}'", query.SequenceName));
            }
            //序列号
            if (!string.IsNullOrWhiteSpace(query.SequenceNumber))
            {
                sql.Append(string.Format(@" and b.SequenceNumber like '%{0}%'", query.SequenceNumber));
            }
            var total = SqlPaging.SqlPaing(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var table = SqlPaging.TableInfo(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var result = new TerminationInfo()
            {
                TotalInfo = total,
                Data = ModelConvertHelper<TerminationMessageInfo>.ConvertToModel(table).ToList() ?? null
            };
            return JsonDate(result);
        }

        /// <summary>
        /// 草稿信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetDraftInfo(QueryInfo query)
        {
            //当前人
            var emplId = (User.Identity as Appkiz.Library.Security.Authentication.AppkizIdentity).Employee.EmplID;

            var sql = new StringBuilder();
            sql.Append(string.Format(@"SELECT ROW_NUMBER () OVER (ORDER BY a.MessageCreateTime DESC) number, b.DocumentNumber,
                                    b.DocumentTitle,
                                    b.ClosedOrHairTime,
                                    b.MessageId,
                                    b.WorkFlowId,
                                    --流程类型
                                    a.MessageTitle,
                                    a.MessageCreateTime AS CreateTime,
                                    a.MessageFinishTime AS FinishTime
                                     FROM WKF_Message a
                                    INNER JOIN GW_ReceiveFile b
                                    ON a.MessageID = b.MessageId
                                    WHERE a.MessageStatus = 0
                                    and a.MessageIssuedBy = '{0}' 
                                    and a.MessageType = 'file'", emplId));
            if (query.PageNumber <= 0)
            {
                query.PageNumber = 1;
            }
            if (query.PageSize <= 0)
            {
                query.PageSize = 20;
            }
            //公文标题
            if (!string.IsNullOrWhiteSpace(query.Title))
            {
                sql.Append(string.Format(@" and b.DocumentTitle like '%{0}%'", query.Title));
            }
            //文号
            if (!string.IsNullOrWhiteSpace(query.DocumentNumber))
            {
                sql.Append(string.Format(@" and b.DocumentNumber like '%{0}%'", query.DocumentNumber));
            }
            //开始发文时间
            if (query.StartClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime >= '{0}'", query.StartClosedOrHairTime));
            }
            //结束发文时间
            if (query.EndClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" and b.ClosedOrHairTime <= '{0}'", query.EndClosedOrHairTime));
            }
            //开始发起时间
            if (query.StartCreatedTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageCreateTime >= '{0}'", query.StartCreatedTime));
            }
            //结束发起时间
            if (query.EndCreatedTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageCreateTime <= '{0}'", query.EndCreatedTime));
            }
            //开始终止时间
            if (query.StartEndTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageFinishTime >= '{0}'", query.StartEndTime));
            }
            //结束终止时间
            if (query.EndEndTime.HasValue)
            {
                sql.Append(string.Format(@" and a.MessageFinishTime <= '{0}'", query.EndEndTime));
            }
            //流程类型
            if (!string.IsNullOrWhiteSpace(query.WorkFlowId))
            {
                sql.Append(string.Format(@" and b.WorkFlowId = '{0}'", query.WorkFlowId));
            }
            var total = SqlPaging.SqlPaing(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var table = SqlPaging.TableInfo(query.PageSize, query.PageNumber, query.SortColumn, query.SortDirection, sql.ToString());
            var result = new DraftDataInfo()
            {
                TotalInfo = total,
                Data = ModelConvertHelper<DraftInfo>.ConvertToModel(table).ToList() ?? null
            };
            return JsonDate(result);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateReceiveInfo(GW_ReceiveFile info)
        {
            var sql = new StringBuilder();
            sql.Append("Update GW_ReceiveFile set ");
            if (!string.IsNullOrEmpty(info.DocumentNumber) && info.DocumentNumber != "null")
            {
                sql.Append(string.Format(@" DocumentNumber = '{0}'", info.DocumentNumber));
            }
            else
            {
                sql.Append(string.Format(@" DocumentNumber = ''"));
            }
            if (!string.IsNullOrEmpty(info.DocumentTitle) && info.DocumentTitle != "null")
            {
                sql.Append(string.Format(@" ,DocumentTitle = '{0}'", info.DocumentTitle));
            }
            else
            {
                sql.Append(string.Format(@" ,DocumentTitle = ''"));
            }
            if (!string.IsNullOrEmpty(info.ReceivedDocumentUnit) && info.ReceivedDocumentUnit != "null")
            {
                sql.Append(string.Format(@" ,ReceivedDocumentUnit = '{0}'", info.ReceivedDocumentUnit));
            }
            else
            {
                sql.Append(string.Format(@" ,ReceivedDocumentUnit = ''"));
            }
            if (!string.IsNullOrEmpty(info.SequenceName) && info.SequenceName != "null")
            {
                sql.Append(string.Format(@" ,SequenceName = '{0}'", info.SequenceName));
            }
            else
            {
                sql.Append(string.Format(@" ,SequenceName = ''"));
            }
            if (!string.IsNullOrEmpty(info.SequenceNumber) && info.SequenceNumber != "null")
            {
                sql.Append(string.Format(@" ,SequenceNumber = '{0}'", info.SequenceNumber));
            }
            else
            {
                sql.Append(string.Format(@" ,SequenceNumber = ''"));
            }
            if (info.ClosedOrHairTime.HasValue)
            {
                sql.Append(string.Format(@" ,ClosedOrHairTime = '{0}'", info.ClosedOrHairTime));
            }

            if (!string.IsNullOrWhiteSpace(info.MessageId))
            {
                sql.Append(string.Format(@" where MessageId = '{0}'", info.MessageId));
            }
            else
            {
                return Json(false);
            }
            SqlHelper.ExcuteNonQuery(sql.ToString(), null);

            return Json(true);
        }

        /// <summary>
        /// 收文序列列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetSequenceInfo()
        {
            var sql = new StringBuilder();
            sql.Append(string.Format(@"select Name from 收文序列 order by ID ASC"));
            return Json(ModelConvertHelper<SequenceName>.ConvertToModel(SqlHelper.ExcuteDataQuery(sql.ToString())));
        }


        /// <summary>
        /// 获取流程统计信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetWorkflowTypeStatistics()
        {
            //当前人
            var emplId = (User.Identity as Appkiz.Library.Security.Authentication.AppkizIdentity).Employee.EmplID;
            var data = _service.GetWorkflowTypeStatistics(emplId);

            return Json(data);
        }

        /// <summary>
        /// 待处理总数
        /// </summary>
        /// <param name="emplId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult TodoNumber()
        {
            //当前人
            var emplId = (User.Identity as Appkiz.Library.Security.Authentication.AppkizIdentity).Employee.EmplID;
            var data = _service.TodoNumber(emplId);
            return Json(new
            {
                Succeed = true,
                Data = new
                {
                    Total = data
                }
            });
        }
    }
}